package com.spice.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.spice.common.result.Response;
import com.spice.common.result.ResultCodeEnum;
import com.spice.common.util.CheckUtil;
import com.spice.common.util.DateUtil;
import com.spice.common.util.MD5;
import com.spice.service.entity.form.RegisterForm;
import com.spice.service.entity.pojo.User;
import com.spice.service.mapper.UserMapper;
import com.spice.service.service.RegisterService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @author MSI-PC
 */
@Slf4j
@Service
public class RegisterServiceImpl implements RegisterService {
//    String defaultAvatar = "https://obuivy.oss-cn-shanghai.aliyuncs.com/logo.png";

//    @Autowired
//    UserMapper userMapper;
//    @Autowired
//    RedisTemplate<String,Object> redisTemplate;


    @Override
    public Response register(RegisterForm registerForm) {
        System.out.println("register");
        return null;
//        if (CheckUtil.objectsHasNull(
//                registerForm.getUsername(),
//                registerForm.getVerificationCode(),
//                registerForm.getPassword()
//                )){
//            return Response.setResult(ResultCodeEnum.PARAM_EMPTY);
//        }
//        if (redisTemplate.hasKey(registerForm.getUsername())) {
//            if (Objects.equals(redisTemplate.opsForValue().get(registerForm.getUsername()), registerForm.getVerificationCode())){
//                redisTemplate.delete(registerForm.getUsername());
//                User user = new User();
//                if (CheckUtil.isPhoneNumber(registerForm.getUsername())){
//                    QueryWrapper<User> queryWrapper = new QueryWrapper<>();
//                    queryWrapper.eq("username",registerForm.getUsername());
//                    if (userMapper.selectCount(queryWrapper) == 0){
//                        user.setUsername(registerForm.getUsername());
//                    } else {
//                        Response.setResult(ResultCodeEnum.PARAM_ERROR).message("该手机号码已被注册");
//                    }
//                } else {
//                    return Response.setResult(ResultCodeEnum.PARAM_ERROR).message("手机号码参数不合法");
//                }
//                if (registerForm.getPassword().length() > 5 && registerForm.getPassword().length() < 17){
//                    user.setPassword(MD5.encrypt(registerForm.getPassword()));
//                } else {
//                    return Response.setResult(ResultCodeEnum.PARAM_ERROR).message("密码参数不合法（6到16位）");
//                }
//                user.setCreateTime(DateUtil.getNowDateTime());
//                user.setAvatar(defaultAvatar);
//                if (!CheckUtil.objectsHasNull(
//                        registerForm.getNickname()
//                )){
//                    if (registerForm.getNickname().length() > 1 && registerForm.getNickname().length() < 17){
//                        user.setNickname(registerForm.getNickname());
//                    } else {
//                        return Response.setResult(ResultCodeEnum.PARAM_ERROR).message("昵称参数不合法（2到16位）");
//                    }
//                }
//                log.info("正在插入user数据:" + user.toString());
//                int insert = userMapper.insert(user);
//                if (insert > 0){
//                    log.info("已经完成插入，插入量:" + insert);
//                    return Response.ok().message("注册成功");
//                } else {
//                    return Response.error().message("注册失败，插入数据错误");
//                }
//            } else {
//                return Response.error().message("短信验证码不正确");
//            }
//        } else {
//            return Response.error().message("服务器未发送过该手机号码的短信或短信已经过期");
//        }
    }
}
